#cmake file for dependencies 
#author:guyadong
#created:2018/05/10
cmake_minimum_required( VERSION 2.6 )
# 依赖代码位置
set(DEPENDENT_SOURCE_DIR "${PROJECT_BINARY_DIR}/dependent_sources" CACHE STRINGS "dependent source folder" FORCE)
#定义公共代码位置   
set( COMMONS_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/common_source_cpp)
if(NOT EXISTS ${COMMONS_SOURCE_DIR}/CMakeLists.txt )
	message( FATAL_ERROR "Not exists or Empty folder: ${COMMONS_SOURCE_DIR},the submodule must be init and pull" )	
endif()

if(WITH_STATIC_CRT)
	set(USE_THRIFT_MD_LIBRARY OFF PARENT_SCOPE)
else()
	set(USE_THRIFT_MD_LIBRARY ON PARENT_SCOPE)
endif()
##############定义thrift位置##############
macro(DEFINE_THRIFT_HOME)
		MESSAGE(STATUS "CMAKE_CXX_COMPILER_VERSION=${CMAKE_CXX_COMPILER_VERSION}")
	if(WIN32)
		if(CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|x86_64")
			set(_arch "x64")
		elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "x86")
		  set(_arch "win32")
		endif()
		if(MSVC)
			if(NOT DEFINED ENV{VisualStudioVersion})
		    # 没有找到 VisualStudioVersion 环境变量
		    message(FATAL_ERROR "not defined environment variable:VisualStudioVersion")  
			endif()
			string(SUBSTRING $ENV{VisualStudioVersion} 0 2 _msvc_version)
			set(_msvc_version vc${_msvc_version}0)
			set (THRIFT_HOME ${CMAKE_CURRENT_LIST_DIR}/dist/thrift-dev/${_msvc_version}/${_arch})
			set (THRIFT_HOME "${THRIFT_HOME}" PARENT_SCOPE)
			unset(_msvc_version)
		elseif(CMAKE_COMPILER_IS_GNUCXX)
			## MinGW
			string(REPLACE "." "" _mingw_version "${CMAKE_CXX_COMPILER_VERSION}" )
			set (THRIFT_HOME ${CMAKE_CURRENT_LIST_DIR}/dist/thrift-dev/gcc${_mingw_version}/${_arch})
			set (THRIFT_HOME "${THRIFT_HOME}" PARENT_SCOPE)
			unset(_mingw_version)
		else()
			message(FATAL_ERROR "TODO:unimplement code")
		endif()
		unset(_arch)
	else()
		#message(FATAL_ERROR "TODO:unimplement code")
	endif()
	
endmacro(DEFINE_THRIFT_HOME)

# 定义boost位置
# BOOST_ROOT BOOST_LIBRARYDIR
macro(DEFINE_BOOST_ROOT)
	if(MSVC)
		set(_boost_root ${CMAKE_CURRENT_LIST_DIR}/dist/boost_1_62_0)
		if(IS_DIRECTORY "${_boost_root}")
			set(BOOST_ROOT "${_boost_root}")
			# 将值传递到上一级
			set(BOOST_ROOT "${BOOST_ROOT}" PARENT_SCOPE)
			message(STATUS "BOOST_ROOT=${BOOST_ROOT}")
		endif()
		if(NOT DEFINED ENV{VisualStudioVersion})
	    # 没有找到 VisualStudioVersion 环境变量
	    message(FATAL_ERROR "not defined environment variable:VisualStudioVersion")  
		endif()
		string(SUBSTRING $ENV{VisualStudioVersion} 0 2 _msvc_version)
		set(_msvc_version msvc-${_msvc_version}.0)
		if(CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|x86_64")
			set(_arch "64")
		elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "x86")
		  set(_arch "32")
		endif()
		set(_boost_librarydir ${_boost_root}/lib${_arch}-${_msvc_version})
		if(IS_DIRECTORY "${_boost_librarydir}")
			set(BOOST_LIBRARYDIR "${_boost_librarydir}")
			# 将值传递到上一级
			set(BOOST_LIBRARYDIR "${BOOST_LIBRARYDIR}" PARENT_SCOPE)
			message(STATUS "BOOST_LIBRARYDIR=${BOOST_LIBRARYDIR}")
		endif()
		unset(_boost_root)
		unset(_msvc_version)
		unset(_arch)
	endif(MSVC)
endmacro(DEFINE_BOOST_ROOT)

DEFINE_THRIFT_HOME()
if(IS_DIRECTORY "${THRIFT_HOME}")
	message(STATUS "THRIFT_HOME=${THRIFT_HOME}") 
	list(APPEND CMAKE_PREFIX_PATH ${THRIFT_HOME})
	# 将值传递到上一级
	set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH}" PARENT_SCOPE)
	message(STATUS "CMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}" )
endif()

DEFINE_BOOST_ROOT()

foreach( subdir common_source_cpp )
	if( IS_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/${subdir}" )
		MESSAGE( STATUS "Found sub project ${subdir}, adding it" )
		add_subdirectory( ${subdir} )
	endif()
endforeach()
